+function($){
  var deletedialog;
  var dialogContent = '<form id="shopupdateForm" method="post"><ul>'+
      '<input type="hidden" name="shopid" id="shopid">'+
      '<li><laber class="list-laber">店名：</laber><input name="shopname" id="shopname" class="list-input"></li>'+
      '<li><laber class="list-laber">老板娘：</laber><input name="shopboss" id="shopboss" class="list-input"></li>'+
      '<li><laber class="list-laber">联系电话：</laber><input name="shoptel" id="shoptel" class="list-input"></li>'+
      '<li><laber class="list-laber">合作品牌：</laber><span id="shopbrand" class="J_selectbranditem"></span><a href="javascript:;" style="line-height:27px;" class="selectbrand">编辑</a></li>'+
      '<li><laber class="list-laber">其他公司品牌：</laber><input name="shopotherbrand" id="shopotherbrand" class="list-input"></li>'+
      '<li><laber class="list-laber">店内信息：</laber><textarea name="shopinfo" class="list-textarea" id="shopinfo" rows="4" cols="34"></textarea></li>'+
      '<li><laber class="list-laber">收货人：</laber><input name="shopconsignee" id="shopconsignee" class="list-input"></li>'+
      '<li><laber class="list-laber">收货人电话：</laber><input name="shopctel" id="shopctel" class="list-input"></li>'+
      '<li><laber class="list-laber">地址：</laber><input name="shopaddr" id="shopaddr" class="list-input"></li>'+
      '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="shopUpdate">修　改</button></div>'+
      '</ul></form>';
  $('body').on('click', '.updateShopCallback',function(){
     callbackUpdate($(this));
   });
  //删除回访记录
  $('body').on('click', '.deleteShopCallback',function(){
    var content = '<span style="color:#d00">确定要删除这一条回访记录吗？</span>'+
                '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="callbackDelete" value="'+$(this).data('id')+'">确定</button><button style="margin-left:30px;" class="btn btn-sm btn-primary" id="closeDelete">取消</button></div>';
      deletedialog = dialog({
        title:'温馨提示',
        content: content,
        width:'300px',
        skin:'  dialog-box',
        lock:true,
        padding:'10px'
      }).showModal();
   });
  $('body').on('click', '#brandtecount', function() {
    window.location.href='/shop/brandtecount?shopid='+$(this).data('shopid');
  });
  $('body').on('click', '#closeDelete', function() {
     deletedialog.close();
   });
  $('body').on('click', '#callbackDelete', function() {
      $.post("/shop/shop_callback_delete",{
        id:$(this).val()
      },function(data){
          location.reload(true);
      });
   });
   //修改回访记录
  $('body').on('click', '#callbackUpdate',function(){
      $.post("/shop/shop_callback_update",{
        id:$('#callbackUpdate').val(),
        val:$('#shopcallback').val()
      },function(){
          location.reload(true);
      });
   });
  //添加回访记录
  $('body').on('click', '#Callback', function() {
    var shopid = $(this).data('shopid');
    var str = '<ul>'+
    '<li><laber class="list-laber">回访内容：</laber><textarea name="shopcallback" class="list-textarea" id="shopcallback" rows="8" cols="38"></textarea></li>'+
    '<div class="dialog-button"><button class="btn btn-sm btn-primary" data-shopid="'+shopid+'" id="addCallback">添 加</button></div>'+
    '</ul>';
    var d = dialog({
      title:'添加回访',
      content: str,
      width:'450px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal();
  });
  $('body').on('click', '#addCallback', function() {
    saveShopCallback($(this).data('shopid'));
  });
  //修改店铺信息
  $('body').on('click', '#shopUpdate', function() {
    var postdata = $('#shopupdateForm').serialize();
    var bid = '';
    var bname = '';
    for (var i = 0; i < window.brandcount.length; i++) {
      if(window.brandcount[i]){
        bid = bid+','+window.brandcount[i];
      }
    }
    bid = bid.replace(',','');
    for (var i = 0; i < window.brandnamecount.length; i++) {
      if(window.brandnamecount[i]){
        bname = bname+','+window.brandnamecount[i];
      }
    }
    bname = bname.replace(',','');
    postdata = postdata+'&shopbrand='+bid+'&shopbrandname='+bname;
    var flag=true;
    $('.list-input,.list-textarea').each(function(index, el) {
      if($(this).val()==''){
        $(this).addClass('none');
        flag=false;
      }
    });
    if(flag==false){
      alert('请将信息补充完整');
      return false;
    }
    $.post('/shop/shop_update_go', postdata, function(data) {
      if(data==1){
        alert('修改成功');
        window.location.reload();
      }
    });
    return false;
  });
  $('body').on('click', '#dialogUpdate', function() {
    mdialog($(this));
  });
  $('body').on('click', '#deleteshop', function() {
    var content = ''+
                  '<span class="warmin-txt">&nbsp;&nbsp;&nbsp;&nbsp;注意！您正在删除店铺！！！删除后与其相关的数据也会一并删除并无法恢复，请先做好备份</span>'+
                  '<div class="btn-box"><button class="btn btn-danger btn-xs" data-shopid='+$(this).data('shopid')+' id="deleteshop_go">确定</button> <button class="btn btn-xs">取消</button></div>';
    deletedialog = dialog({
      title:'注意',
      content: content,
      width:'400px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal();
  });
  $('body').on('click', '#deleteshop_go', function() {
    $.post('/shop/shopdelete', {shopid: $(this).data('shopid')}, function(data) {
      if(data!='false'){
        alert('删除成功');
        window.location.href = "/shop/shop_list";
      }else{
        alert('删除失败');
        location.reload(true);
      }
    });
  });
  var dialoginit = function($this){
    var $pare = $this.parent();
    $('#shopid').val($('#J_shopid').text());
    $('#shopname').val($('#J_shopname').text());
    $('#shopboss').val($('#J_shopboss').text());
    $('#shoptel').val($('#J_shoptel').text());
    $('#shopbrand').text($('#J_shopbrand').text());
    $('#shopotherbrand').val($('#J_shopotherbrand').text());
    $('#shopinfo').val($('#J_shopinfo').text());
    $('#shopconsignee').val($('#J_shopconsignee').text());
    $('#shopctel').val($('#J_shopctel').text());
    $('#shopaddr').val($('#J_shopaddr').text());
    $.get('/brand/getshopbrand',{shopid:$('#J_shopid').text()}, function(data) {
      window.brandcount.splice(0,window.brandcount.length);
      window.brandnamecount.splice(0,window.brandnamecount.length);
      var json = JSON.parse(data);
      window.j=json.length;
      for (var i = 0; i < json.length; i++) {
        window.brandcount[i]=parseInt(json[i].bid);
        window.brandnamecount[i]=json[i].bname;
      }
    });
  }
  var mdialog = function($this){
    var d = dialog({
      title:'修改店铺信息',
      content: dialogContent,
      width:'450px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal(dialoginit($this));
  }
  //修改回访记录
  var callbackUpdate = function($this){
     var str = '<ul>'+
      '<li><laber class="list-laber">回访内容：</laber><textarea name="shopcallback" class="list-textarea" id="shopcallback" rows="8" cols="38"></textarea></li>'+
      '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="callbackUpdate">修　改</button></div>'+
      '</ul>';
      var d = dialog({
        title:'修改回访内容',
        content: str,
        width:'450px',
        skin:'  dialog-box',
        lock:true,
        padding:'10px'
      }).showModal(callbackUpdateinit($this));
   }
  var callbackUpdateinit = function($this){
    $('#shopcallback').val($this.parent().prevAll('.J_callback').html());
    $('#callbackUpdate').val($this.data('shopid'));
  }
  var saveShopCallback = function(shopid){
    if($('#shopcallback').val()==""){
      alert('请先填写回访内容');
    }else{
      $.get("/shop/shop_callback_go",{
        shopid:shopid,
        val:$('#shopcallback').val()
      },function(data){
        if(data=="true"){
          location.reload(true);
        }
      });
    }
  }
}(jQuery);
